<template>
  <transition name="down" @after-leave="destroy">
    <div
      v-show="isVisible"
      class="min-w-[420px] fixed top-[20px] left-[50%] translate-x-[-50%] z-50 flex items-center px-3 py-1.5 rounded-sm border cursor-pointer"
      :class="styles[type].containerClass"
    >
      <m-svg-icon
        :name="styles[type].icon"
        :fillClass="styles[type].fillClass"
        class="h-1.5 w-1.5 mr-2"
      ></m-svg-icon>
      <span class="text-sm" :class="styles[type].textClass">
        {{ content }}
      </span>
    </div>
  </transition>
</template>

<script>
const typeEnum = ['success', 'warn', 'error']
</script>
<script setup>
import mSvgIcon from '../svg-icon/index.vue'
import { onMounted, ref } from 'vue'
const props = defineProps({
  type: {
    type: String,
    required: true,
    validator(val) {
      const res = typeEnum.includes(val)
      if (!res) {
        throw new Error(`类型必须为 ${typeEnum.join(',')}其中之一`)
      }
      return res
    }
  },
  // 文本
  content: {
    type: String,
    required: true
  },
  // 展示市场
  duration: {
    type: Number,
    default: 3
  },
  // 关闭回调
  destroy: {
    type: Function
  }
})
// 样式表数据
const styles = {
  warn: {
    icon: 'warn',
    fillClass: 'fill-warn-300',
    textClass: 'text-warn-300',
    containerClass:
      'bg-warn-100 border-warn-200  hover:shadow-lg hover:shadow-warn-100'
  },
  error: {
    icon: 'error',
    fillClass: 'fill-error-300',
    textClass: 'text-error-300',
    containerClass:
      'bg-error-100 border-error-200  hover:shadow-lg hover:shadow-error-100'
  },
  success: {
    icon: 'success',
    fillClass: 'fill-success-300',
    textClass: 'text-success-300',
    containerClass:
      'bg-success-100 border-success-200  hover:shadow-lg hover:shadow-success-100'
  }
}
const isVisible = ref(false)
const duration = '0.5s'
const initShow = () => {
  isVisible.value = true
  setTimeout(() => {
    isVisible.value = false
  }, parseInt(duration.replace('0.', '').replace('s', '')) * 1000)
}
onMounted(initShow)
</script>

<style lang="scss" scoped>
.down-enter-active,
.down-leave-active {
  transition: all v-bind(duration);
}

.down-enter-from,
.down-leave-to {
  opacity: 0;
  transform: translate3d(-50%, -100px, 0);
}
</style>
